package com.spzx.order.configure;


import org.bouncycastle.pqc.crypto.newhope.NHOtherInfoGenerator;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DeadLetterMqConfig {

    // 声明一些变量
    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    // 定义交换机
    @Bean
    public DirectExchange createDeadExchange(){
        return new DirectExchange(exchange_dead, true, false);
    }

    //定义死信队列
    @Bean
    public Queue createDeadQueue(){
        // 设置如果队列一 出现问题，则通过参数转到exchange_dead，routing_dead_2 上！
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", exchange_dead);
        args.put("x-dead-letter-routing-key", routing_dead_2);
        args.put("x-message-ttl", 10000);
        return new Queue(queue_dead_1, true, false, false, args);
    }

    //定义普通队列
    @Bean
    public Queue createQueue(){
        return new Queue(queue_dead_2, true);
    }

    //定义死信队列绑定关系
    @Bean
    public Binding createDeadBinding(){
        return BindingBuilder.bind(createDeadQueue()).to(createDeadExchange()).with(routing_dead_1);
    }

    //定义普通队列绑定关系
    @Bean
    public Binding createBinding(){
        return BindingBuilder.bind(createQueue()).to(createDeadExchange()).with(routing_dead_2);
    }
}
